//console.log(location.href.split("?")[1])
//switch (parseInt(location.href.split("?")[1])) {
//    case 0:
//        $(".logo").html("热销榜");
//        break;
//    case 1:
//        $(".logo").html("新书榜");
//        break;
//    case 2:
//        $(".logo").html("点击榜");
//        break;
//    case 3:
//        $(".logo").html("连载榜");
//        break;
//    case 4:
//        $(".logo").html("完本榜");
//        break;
//    default:
//        break;
//}
$("nav>li").on("click", function () {
    $(window).scrollTop(0);
    $("nav>li").removeClass();
    $(".homepage_list").attr("check", "false").css("display", "none");
    $(this).addClass("active");
    $(".homepage_list").eq($(this).index()).attr("check", "true").css("display", "block");
    $("nav>li").find("a").css("display", "none")
    $(this).find("a").css("display", "block")
    var index = $(this).index();
    var add_index = index + 1;
    var page = $(this).attr('data-page');
    var action;
    if(type == 'hot'){
        action = 'HotSaleRank';
    }else if(type == 'new'){
        action = 'NewBookRank';
    }else if(type == 'click'){
        action = 'ClickRank';
    }else if(type == 'continue' || type == 'all'){
        action = 'BookTypeRank';
    }
    var url;
    var isFinish = 0;
    if(type == 'continue' || type == 'all'){
        if(type == 'continue'){
            isFinish = 2;
        }else{
            isFinish = 1;
        }
        url = "novel/novel/"+action+"?type="+add_index+"&isFinish="+isFinish;
    }else{
        url = "novel/novel/"+action+"?type="+add_index;
    }
    window.history.pushState({},0,'http://'+window.location.host+'/'+url);
    var cached = $(".homepage_list").eq(index).attr('data-cached');
    if (cached != 'true') {
        $(".homepage_list").eq(index).find("li").remove();
        getBook(add_index, page, isFinish,action);
    }
})
console.log($(".homepage_list").height())
//点击书籍进入对应的书籍详情
$(".homepage_list>li").on("click", function () {
    location.href = "../html/read_Info.html"
})
//跳转搜索页面
$(".search_hot").on("click", function () {
    location.href = "../novel/search"
})
//返回上一页面
$(".return_back").on("click", function () {
    window.history.back();
})
//分类遮罩显示隐藏
$(".classifty_btn").on("click", function () {
    if ($(this).attr("check") == "false") {
        $(this).attr("check", "true");
        $(".classifty").css("display", "block");
        $("body").css("overflow", "hidden");
        $(".classifty_nav>li").eq(0).on("click", function (e) {
            var e = e || event;
            e.stopPropagation();
            location.href = "../novel/newBook";
        })
        $(".classifty_nav>li").eq(1).on("click", function (e) {
            var e = e || event;
            e.stopPropagation();
            location.href = "../novel/rankBook";
        })
        $(".classifty_nav>li").eq(2).on("click", function (e) {
            var e = e || event;
            e.stopPropagation();
            location.href = "../html/classifty.html";
        })
        $(".classifty_nav>li").eq(3).on("click", function (e) {
            var e = e || event;
            e.stopPropagation();
            location.href = "../novel/freeBook";
        })
    } else {
        $(".classifty").css("display", "none");
        $(this).attr("check", "false");
        $("body").css("overflow", "auto")
    }
})
$(".classifty").on("click", function () {
    $(".classifty_btn").trigger("click")
})

//加载更多筛选书籍
$(window).on("scroll",function(){
    if($(this).scrollTop()>=$(document).height()-$(window).height() ){
        // 加载剩余书籍
        var index = 0;
        var page = 1;
        for (var i = 0; i < $("nav>li").length; i++) {
            if ($("nav>li").eq(i).hasClass("active")) {
                index = i;
                page = $("nav>li").eq(i).attr('data-page');
            }
        }
        var add_index = index + 1;
        var action;
        if(type == 'hot'){
            action = 'HotSaleRank';
        }else if(type == 'new'){
            action = 'NewBookRank';
        }else if(type == 'click'){
            action = 'ClickRank';
        }else if(type == 'continue' || type == 'all'){
            action = 'BookTypeRank';
        }
        var url;
        var isFinish = 0;
        if(type == 'continue' || type == 'all'){
            if(type == 'continue'){
                isFinish = 2;
            }else{
                isFinish = 1;
            }
            url = "novel/novel/"+action+"?type="+add_index+"&isFinish="+isFinish;
        }else{
            url = "novel/novel/"+action+"?type="+add_index;
        }
        window.history.pushState({},0,'http://'+window.location.host+'/'+url);
//        var cached = $(".homepage_list").eq(index).attr('data-cached');
//        if (cached != 'true') {
//            $(".homepage_list").eq(index).find("li").remove();
            getBook(add_index, page, isFinish,action);
//        }
    }
})

function getBook(index, page, isFinish, action) {
    var h = $(".hide").eq(index-1).val();
    if(h == 0){
        return false;
    }
    var url = "/novel/novel/"+action;
    if(type == "continue" || type == "all"){
        var d = {type: index, page: page, isFinish: isFinish};
    }else{
        d = {type: index, page: page};
    }
    $.ajax({
        url: url,
        type: "post",
        dataType: "json",
        data: d,
        success: function (data) {
            if (data.status == 0) {
                var pageNum = data.data.params.pageNum;
                if(pageNum <= page){
                    $(".hide").eq(index-1).val('0');
                }
                var list = data.data.list.list;
                $("nav>li").eq(index-1).attr('data-page', data.data.params.page);
                var str = '';
                for (i in list) {
                    str += '<li data-id="' + list[i]['novel_id'] + '">' +
                                '<img src="' + list[i]['novel_cover'] + '">' +
                                '<div>' +
                                    '<h4>' + list[i]['novel_title'] + '</h4>' +
                                    '<h6>' + list[i]['novel_introduction'] + '</h6>' +
                                    '<p>' +
                                        '<span>作者 ' + list[i]['novel_author'] + '</span>' +
                                        '<a href="javascript:;">' + list[i]['novel_type'] + '</a>' +
                                        '<a href="javascript:;">' + list[i]['novel_category'] + '</a>' +
                                    '</p>' +
                                '</div>' +
                                '<a href="javascript:;"></a>' +
                            '</li>';
                }
                $(".homepage_list").eq(index-1).append(str);
                $(".homepage_list").eq(index-1).attr('data-cached', true);
                //点击书籍进入对应的书籍详情
                $(".homepage_list>li").on("click", function () {
                    var nid = $(this).attr('data-id');
                    location.href = "../novel/novelInfo?novelid="+nid;
                })
            } else {
                $(".message_info_alert>span").html("系统繁忙请稍后再试");
                $(".message_info_alert").fadeIn(function () {
                    setTimeout(function () {
                        $(".message_info_alert").fadeOut()
                        window.location.reload();
                    }, 300)
                })
            }
        },
        error: function (e) {
            $(".message_info_alert>span").html("系统繁忙请稍后再试");
            $(".message_info_alert").fadeIn(function () {
                setTimeout(function () {
                    $(".message_info_alert").fadeOut()
                    window.location.reload();
                }, 300)
            })
        }
    });
}